﻿// src/crudOption/service/serviceList.js
export default () => {
  const hasDuplicateValues = (arr, prop) => {
    const valueSet = new Set();
    return arr.some(obj => {
      const value = obj[prop];
      if (valueSet.has(value)) return true;
      valueSet.add(value);
      return false;
    });
  };
  // ===== 搜索 =====
  const searchOption = ref({
    formConfig: {
      id: { label: "应用ID", component: "el-input" },
      name: { label: "应用名称", component: "el-input" },
      apiCategoryId: {
        label: "应用类别",
        component: "el-select",
        defaultValue: "",
        dicData: [],
        dicProps: { label: "label", value: "value" },
        placeholder: "全部",
        clearable: true,
      },
      type: {
        label: "应用类型",
        component: "el-select",
        defaultValue: "",
        dicData: [],
        dicProps: { label: "label", value: "value" },
        placeholder: "全部",
        clearable: true,
      },
      // 新增时间
      createTime: {
        label: "新增时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
      },
      // 编辑时间
      updateTime: {
        label: "编辑时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
      },
      // 编辑人
      updateName: {
        label: "操作人员",
      },
    },
  });

  // ===== 表格/表单 =====
  const tableOption = ref(
    Utils.tools.deleteSearchConfig({
      addTitle: "新增应用",
      editTitle: "编辑应用",
      dialogWidth: "35%", // 20% 在窄屏可能太小，这里给个像素更稳；按需改回
      align: "left",
      menu: true,
      menuWidth: 100,
      addBtn: false,
      editBtn: false,
      delBtn: false,

      /**
       * 弹窗表单（与原型一致）
       */
      group: [
        {
          column: {
            // 1) 应用编码≤100 字
            code: {
              label: "应用编码",
              span: 24,
              type: "input",
              maxlength: 100,
              labelWidth: 110,
              labelPosition: "right",
              placeholder: "请输入应用编码",
              rules: [{ required: true, message: "请输入应用编码", trigger: "blur" }],
            },
            // 2) 应用logo）
            logoUrl: {
              label: "应用logo",
              span: 24,
              labelWidth: 110,
              labelPosition: "right",
              rules: [{ required: true, message: "请上传应用logo", trigger: "blur" }],
            },
            // 3) 应用类别
            apiCategoryId: {
              label: "应用类别",
              span: 24,
              type: "select",
              placeholder: "请选择应用类别",
              labelWidth: 110,
              labelPosition: "right",
              dicData: [], // ← 页面里动态填
              dicProps: { label: "label", value: "value" },
              rules: [{ required: true, message: "请选择应用类别", trigger: "blur" }],
            },
            // 4) 应用名称 ≤10 字）
            name: {
              label: "应用名称",
              span: 24,
              placeholder: "请输入应用名称",
              labelPosition: "right",
              type: "input",
              maxlength: 10,
              labelWidth: 110,
              clearable: true,
              rules: [{ required: true, message: "请输入应用名称", trigger: "blur" }],
            },
            // 5) 应用类型）
            type: {
              label: "应用类型",
              span: 24,
              type: "select",
              placeholder: "请选择应用类型",
              labelWidth: 110,
              labelPosition: "right",
              dicData: [], // ← 页面里动态填
              dicProps: { label: "label", value: "value" },
              rules: [{ required: true, message: "请选择应用类型", trigger: "blur" }],
            },
            // 6) 商家是否可用
            useShop: {
              label: "商家是否可用",
              span: 24,
              type: "select",
              placeholder: "请选择商家是否可用",
              labelPosition: "right",
              labelWidth: 110,
              dicData: [
                { label: "是", value: "1" },
                { label: "否", value: "0" },
              ],
              rules: [{ required: true, message: "请选择商家是否可用", trigger: "blur" }],
            },
            // 7) 类目排序（必填 正整数，越小排越前）
            cost: {
              label: "应用成本",
              span: 24,
              placeholder: "请输入应用成本",
              labelWidth: 110,
              labelPosition: "right",
              append: "元/次",
              rules: [
                { required: true, message: "请输入正确应用成本", trigger: "blur" },
                {
                  validator: (_r, v, cb) => (v > 0 ? cb() : cb(new Error("请输入正数"))),
                  trigger: "blur",
                },
              ],
            },
            // 8) 品牌资费 正整数）
            fee: {
              label: "品牌资费",
              span: 24,
              placeholder: "请输入品牌资费",
              labelWidth: 110,
              labelPosition: "right",
              append: "元/次",
              rules: [
                { required: true, message: "请输入品牌资费", trigger: "blur" },
                {
                  validator: (_r, v, cb) => (v > 0 ? cb() : cb(new Error("请输入正数"))),
                  trigger: "blur",
                },
              ],
            },
            // 9) 应用描述
            description: {
              label: "应用描述",
              span: 24,
              maxlength: 100,
              placeholder: "请输入应用描述",
              type: "textarea",
              labelWidth: 110,
              labelPosition: "right",
              showWordLimit: true,
              minRows: 3,
              maxRows: 4,
              rules: [{ required: true, message: "请输入应用描述", trigger: "blur" }],
            },
            // 10) 应用参数）
            configParams: {
              label: "应用参数",
              labelWidth: 110,
              labelPosition: "right",
              span: 24,
              type: "array",
              rules: [
                { required: true, message: "请添加应用参数", trigger: "blur" },
                {
                  validator: (rule, value, callback) => {
                    console.log("值", value);
                    const CHINESE_REGEX = /[\u4e00-\u9fff]/;
                    const hasAllValue = value.every(item => item.value && item.key);
                    const hasChinese = value.every(item => !CHINESE_REGEX.test(item.key));
                    const hasDupValues = hasDuplicateValues(value, "key");
                    if (!hasAllValue) {
                      callback(new Error("参数键值不能为空"));
                    } else if (!hasChinese) {
                      callback(new Error("参数键名不能为中文"));
                    } else if (hasDupValues) {
                      callback(new Error("参数键名不能重复"));
                    } else {
                      callback();
                    }
                  },
                  trigger: "blur",
                },
              ],
            },
          },
        },
      ],

      /**
       * 列表列
       */
      column: {
        // 隐藏字段：parentId（“加下级”时由页面赋值）
        id: { label: "应用ID", width: 120, type: "number", display: false },

        name: { label: "应用名称", minWidth: 160, display: false },

        apiCategoryId: {
          label: "应用类别",
          dicData: [], // ← 页面里动态填
          dicProps: { label: "label", value: "value" },
          width: 150,
          type: "number",
          display: false,
        },

        type: {
          label: "应用类型",
          dicData: [], // ← 页面里动态填
          dicProps: { label: "label", value: "value" },
          width: 150,
          type: "number",
          display: false,
        },

        cost: { label: "应用成本(元)", width: 120, type: "number", display: false },

        fee: { label: "品牌资费(元)", width: 120, type: "number", display: false },

        description: { label: "应用描述", minWidth: 200, type: "number", display: false },

        createTime: {
          label: "新增时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },

        updateTime: {
          label: "最近编辑时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },

        updateName: {
          label: "操作人员",
          minWidth: 120,
          addDisplay: false,
          editDisplay: false,
        },
      },
    })
  );

  return { searchOption, tableOption };
};
